<?php
    class sanphamdb
    {
        public $_tablename = "giaphucvn_sanpham";
        public function getListInCart($ID = '')
		{
			$sql =	"SELECT * ";
			$sql.=	"FROM ".$this->_tablename." ";
			$sql.=	"WHERE MaSanPham IN (" . $ID . ")";
			
			$db = new dataservice();
			return $db->ExecuteQuery($sql);
		}
		
		public function getNumListInCart($ID = '')
		{
			$sql =	"SELECT * ";
			$sql.=	"FROM ".$this->_tablename." ";
			$sql.=	"WHERE MaSanPham IN (" . $ID . ")";
			
			$db = new dataservice();
			return $db->ReturnNumRows($sql);
		}
        
        public function getList($MaSanPham = 0, $TenSanPham = '')
		{
			$sql = "SELECT * FROM ".$this->_tablename." WHERE (1)";
			
			if ($MaSanPham != 0)
				$sql .= " AND MaSanPham = " . dataservice::QuoteSmart($MaSanPham);
			
			if ($TenSanPham != '')
				$sql .= " AND TenSanPham LIKE " . dataservice::QuoteSmart('%' . $TenSanPham . '%');
			//echo $sql;
			$db = new dataservice();
			$ret = $db->ExecuteQuery($sql);
			return $ret;
		}
        
        public function getListNewProduct($SoSanPham = 6)
		{
			$sql =	"SELECT * ";
			$sql .=	"FROM ".$this->_tablename." ";
			$sql .=	"WHERE (1) ";
            $sql .=	"ORDER BY NgayNhap DESC LIMIT 0,".$SoSanPham;
            
			$db = new dataservice();
			return $db->ExecuteQuery($sql);
		}
        
        public function getListDealsProduct($SoSanPham = 6)
		{
			$sql =	"SELECT * ";
			$sql .=	"FROM ".$this->_tablename." ";
			$sql .=	"WHERE (1) AND GiaKhuyenMai != GiaGoc ";
            $sql .=	"ORDER BY NgayNhap DESC LIMIT 0,".$SoSanPham;
            //echo $sql;
			$db = new dataservice();
			return $db->ExecuteQuery($sql);
		}
        
        public function add($TenSanPham = '', $GiaGoc = 0, $GiaKhuyenMai = 0, $HinhNho = '', $HinhTo = '', $MoTa = '', $ChiTiet = '', $HuongDan = '', $SoLuong = 1, $MaDonViTinh = 0, $MaQuyCach = 0, $MaXuatXu = 0, $NgayNhap = '', $MaLoaiSanPham = 0, $MaLoaiSanPhamCap1 = 0, $MaLoaiSanPhamCap2 = 0, $MaLoaiSanPhamCap3 = 0)
		{
			$sql = "INSERT INTO ".$this->_tablename."(TenSanPham, GiaGoc, GiaKhuyenMai, HinhNho, HinhTo, MoTa, ChiTiet, HuongDan, SoLuong, MaDonViTinh, MaQuyCach, MaXuatXu, NgayNhap, MaLoaiSanPham, MaLoaiSanPhamCap1, MaLoaiSanPhamCap2, MaLoaiSanPhamCap3) VALUES(" .
            dataservice::QuoteSmart($TenSanPham) . ", " . dataservice::QuoteSmart($GiaGoc) . ", " . dataservice::QuoteSmart($GiaKhuyenMai). ", " . 
            dataservice::QuoteSmart($HinhNho) . ", " . dataservice::QuoteSmart($HinhTo) . ", " . dataservice::QuoteSmart($MoTa) . ", " . 
            dataservice::QuoteSmart($ChiTiet) . ", " . dataservice::QuoteSmart($HuongDan). ", " . dataservice::QuoteSmart($SoLuong). ", " . 
            dataservice::QuoteSmart($MaDonViTinh). ", " . dataservice::QuoteSmart($MaQuyCach). ", " . dataservice::QuoteSmart($MaXuatXu). ", " . dataservice::QuoteSmart($NgayNhap) . ", " . 
            dataservice::QuoteSmart($MaLoaiSanPham) . ", " . dataservice::QuoteSmart($MaLoaiSanPhamCap1) . ", " . dataservice::QuoteSmart($MaLoaiSanPhamCap2) . ", " . dataservice::QuoteSmart($MaLoaiSanPhamCap3) . ")";
			//echo $sql;
			$db = new dataservice();
			return $db->ExecuteNonQuery($sql);
		}
        
        public function update($MaSanPham = 0, $TenSanPham = '', $GiaGoc = 0, $GiaKhuyenMai = 0, $HinhNho = '', $HinhTo = '', $MoTa = '', $ChiTiet = '', $HuongDan = '', $SoLuong = 1, $MaDonViTinh = 0, $MaQuyCach = 0, $MaXuatXu = 0, $NgayNhap = '', $MaLoaiSanPham = 0, $MaLoaiSanPhamCap1 = 0, $MaLoaiSanPhamCap2 = 0, $MaLoaiSanPhamCap3 = 0)
		{
			$sql  = "UPDATE ".$this->_tablename." ";
            $sql .= "SET TenSanPham = (" . dataservice::QuoteSmart($TenSanPham) . ") ,";
            $sql .= "GiaGoc = (" . dataservice::QuoteSmart($GiaGoc) . "), ";
            $sql .= "GiaKhuyenMai = (" . dataservice::QuoteSmart($GiaKhuyenMai) . "), ";
            $sql .= "HinhNho = (" . dataservice::QuoteSmart($HinhNho) . "), ";
            $sql .= "HinhTo = (" . dataservice::QuoteSmart($HinhTo) . "), ";
            $sql .= "MoTa = (" . dataservice::QuoteSmart($MoTa) . "), ";
            $sql .= "ChiTiet = (" . dataservice::QuoteSmart($ChiTiet) . "), ";
            $sql .= "HuongDan = (" . dataservice::QuoteSmart($HuongDan) . "), ";
            $sql .= "SoLuong = (" . dataservice::QuoteSmart($SoLuong) . "), ";
            $sql .= "MaDonViTinh = (" . dataservice::QuoteSmart($MaDonViTinh) . "), ";
            $sql .= "MaQuyCach = (" . dataservice::QuoteSmart($MaQuyCach) . "), ";
            $sql .= "MaXuatXu = (" . dataservice::QuoteSmart($MaXuatXu) . "), ";
            $sql .= "NgayNhap = (" . dataservice::QuoteSmart($NgayNhap) . "), ";
            $sql .= "MaLoaiSanPham = (" . dataservice::QuoteSmart($MaLoaiSanPham) . "), ";
            $sql .= "MaLoaiSanPhamCap1 = (" . dataservice::QuoteSmart($MaLoaiSanPhamCap1) . "), ";
            $sql .= "MaLoaiSanPhamCap2 = (" . dataservice::QuoteSmart($MaLoaiSanPhamCap2) . "), ";
            $sql .= "MaLoaiSanPhamCap3 = (" . dataservice::QuoteSmart($MaLoaiSanPhamCap3) . ") ";
			$sql .= "WHERE MaSanPham = (" . $MaSanPham . ")";
            
			$db = new dataservice();
			$ret = $db->ExecuteNonQuery($sql);
			return $ret;
		}
        
        public function delete($MaSanPham = 0)
        {
            $sql  = "DELETE FROM giaphucvn_sanpham ";
            $sql .= "WHERE (1)";
            $sql .= " AND MaSanPham = (" . $MaSanPham . ")";
            
            $db = new dataservice();
			return $db->ExecuteNonQuery($sql);
        }
        
        public function issetType($TenSanPham = "")
		{
			$sql = "SELECT COUNT(*) AS SoDong FROM ".$this->_tablename." WHERE (1)";
			if($TenSanPham != "")
				$sql .= " AND TenSanPham = '$TenSanPham'";
			
			$data = new dataservice();
			return $data->ExecuteQuery($sql);
		}
        
        public function issetTypes($MaSanPham = 0, $TenSanPham = "")
		{
			$sql = "SELECT COUNT(*) AS SoDong FROM ".$this->_tablename." WHERE (1)";
            
            if($MaSanPham != 0)
				$sql .= " AND MaSanPham = $MaSanPham";
            
			if($TenSanPham != "")
				$sql .= " OR TenSanPham = '$TenSanPham'";
			
			$data = new dataservice();
			return $data->ExecuteQuery($sql);
		}
    }
?>